import axios from 'axios'

import * as UI from './ui'
import * as Resource from './resource'

export { UI, Resource }

export { WINDOW_TYPE, COORD_TYPE } from './_constants'

// 地图初始化
const defaultConfig = {
  username: 'shdemo',
  password: 'Sh@12345',
  appId: 'fd8e2313-1135-4374-b9eb-9b5ff40214e1',
  Authorization:
    'Basic Q2ltQXBwMTozZTgzZmZmMy04MGJmLTRkODgtOTJmNy0zM2U0MzNkYjBmYTE=',
  urlsuffix:
    'grant_type=password&client_id=CimApp1&client_secret=3e83fff3-80bf-4d88-92f7-33e433db0fa1'
}

export function initMap(containerId, params = defaultConfig) {
  const formData = new FormData()
  formData.append('username', params.username)
  formData.append('password', params.password)

  return axios
    .request({
      url: 'http://aicim.com:64321/oauth/token?' + params.urlsuffix,
      method: 'post',
      data: formData,
      headers: {
        Authorization: params.Authorization
      }
    })
    .then(response => {
      const data = response.data
      return new Promise(resolve => {
        window.AC.init({
          elementId: containerId,
          url:
            'http://aicim.com:55505/V5/#/map?appId=' +
            params.appId +
            '&ac_access=' +
            data.access_token,
          callbackFn: resolve
        })
      })
    })
}
